﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.FileSystem
{
    public static class FileSystemExtensions
    {
        public static MediaItem ToMediaItem(this DirectoryInfo dir, string rootDirPath)
        {
            var mi = new MediaItem
            {
                Id = dir.FullName.Replace(rootDirPath, "\\"),
                Title = dir.Name,
                PreviewImageUrl = "",
                Icon = UmbracoIcons.FolderOpen,
                HasChildren = dir.GetDirectories().Length > 0 || dir.GetFiles().Length > 0,
                Selectable = false
            };

            return mi;
        }

        public static MediaItem ToMediaItem(this FileInfo file, string rootDirPath)
        {
            var mi = new MediaItem
            {
                Id = file.FullName.Replace(rootDirPath, "\\"),
                Title = file.Name,
                PreviewImageUrl = "",
                Icon = UmbracoIcons.MediaFile,
                HasChildren = false,
                Selectable = true
            };

            // TODO: Add more meta data
            mi.MetaData.Add("Extension", file.Extension);
            mi.MetaData.Add("Size", String.Format(new FileSizeFormatProvider(), "{0:fs}", file.Length));
            mi.MetaData.Add("Created", file.CreationTime.ToString("dd/MM/yyyy hh:mm:ss"));
            mi.MetaData.Add("Modified", file.LastWriteTime.ToString("dd/MM/yyyy hh:mm:ss"));

            return mi;
        }
    }
}